<?php

/**
 * controler para movimientos de ingreso stock
 *
 * @author jreyes
 */

require_once 'class/im/articulos/DP/ImArticuloDP.class.php';
require_once 'class/im/articulos/FN/ImArticuloFN.class.php';
require_once 'class/common/mc/FN/XxMaestroListaFN.class.php';
require_once 'class/im/fam/FN/ImClasificacionFN.class.php';
require_once 'class/im/fab/FN/ImFabricanteFN.class.php';
require_once 'class/frm/ArticuloFrm.php';

class ArticuloController extends ControllerBase
{
	public function listarArticuloAutocomplete()
	{
		$o_ImArticuloFN= new ImArticuloFN();      
        if ($this->requestVars){
            if (array_key_exists('term', $this->requestVars)) {
                $o_ImArticuloFN->setDescripcion($this->requestVars["term"]);
            }
        }
        $result = $o_ImArticuloFN->select();        
        $vars['result'] = $result;
        if (array_key_exists('modoBusqueda', $this->requestVars)) {
            $vars['modoBusqueda'] = $this->requestVars["modoBusqueda"];
        }else{
            $vars['modoBusqueda'] = "";
        }
        $this->view->show("im/articulos/ArticuloAutocomplete",$vars);
	}
	
	
	public function getObjArticulo()
	{
	   $a_result['rpta'] = 'false';
	   try 
	   {
    		$o_ImArticuloFN= new ImArticuloFN();      
            if ($this->requestVars){
                if (array_key_exists('articuloId', $this->requestVars)) {
                    $o_ImArticuloFN->setArticuloId($this->requestVars["articuloId"]);
                }
            }
            $result = $o_ImArticuloFN->selectByPK();        
            if($result)
            {
              $o_XxMaestroListaFN = new XxMaestroListaFN();
              $o_XxMaestroListaFN->setListaId("tipoprod");   
              $o_XxMaestroListaFN->setValorId($result->getTipo());
		      $o_tipo_articulo		= $o_XxMaestroListaFN->selectByPK();
		      ($o_tipo_articulo)?$s_tipoArticuloName = $o_tipo_articulo->getDescripcion():$s_tipoArticuloName ='';
		      
		      
              $a_result['descripcion']      = $result->getDescripcion();
              $a_result['articuloId']       = $result->getArticuloId();
              $a_result['undMedida']        = $result->getUoM();
              $a_result['marcaId']          = $result->getMarcaId();
              $a_result['activo']           = $result->getActivo();
              $a_result['tipoArticuloId']   = $result->getTipo();
              $a_result['tipoArticuloName'] = $s_tipoArticuloName;
              
              $a_result['rpta']         = 'true';
              $a_result['msg']          = "";
           
            }else{
              $a_result['msg'] = "El producto no esta registrado en la base de datos!!!";
            }
            
	   }catch (Exception $e)
	   {
         $a_result['msg'] = $e->getMessage().' en '.$e->getFile().' linea '.$e->getLine();
	   }
	   
       echo json_encode($a_result);
	}
	
	
	public function listarArticulo(){
		
		$o_ImArticuloFN= new ImArticuloFN();       
		
		$page = isset($this->requestVars['page']) ? $this->requestVars['page'] : 1;
		$rp = isset($this->requestVars['rp']) ? $this->requestVars['rp'] : 10;
		$sortname = isset($this->requestVars['sortname']) ? $this->requestVars['sortname'] : 'name';
		$sortorder = isset($this->requestVars['sortorder']) ? $this->requestVars['sortorder'] : 'desc';		
		$o_ImArticuloFN->page = $page;
		$o_ImArticuloFN->tamPag = $rp;
		$o_ImArticuloFN->sortCampos = $sortname;
		$o_ImArticuloFN->sortType   = $sortorder;
          

          
        if ($this->requestVars){
            if (array_key_exists('articuloId', $this->requestVars)) {
                $o_ImArticuloFN->setArticuloId($this->requestVars["articuloId"]) ;   
            }
            if (array_key_exists('tipoArticulo', $this->requestVars)) {
                $o_ImArticuloFN->setTipo($this->requestVars["tipoArticulo"]);
            }
            if (array_key_exists('descripcion', $this->requestVars)) {
                $o_ImArticuloFN->setDescripcion($this->requestVars["descripcion"]);  
            }           
        }
        $result = $o_ImArticuloFN->select();     
        $vars['result'] = $result;
        $vars['articuloFN'] = $o_ImArticuloFN;
        if (array_key_exists('modoBusqueda', $this->requestVars)) {
            $vars['modoBusqueda'] = $this->requestVars["modoBusqueda"];
        }else{
            $vars['modoBusqueda'] = "";
        }
        $this->view->show("im/articulos/ArticuloLista",$vars);
		
	}
	
    public function listarArticuloForm(){
		if (!isset($vars)){
			$vars = '';
		}        
          $o_XxMaestroListaFN		= new XxMaestroListaFN(); 
          $o_XxMaestroListaFN->setListaId("tipoprod");   
          $a_tipoArticulo		= $o_XxMaestroListaFN->select();      
          $vars['a_tipoArticulo'] = $a_tipoArticulo;
        $this->view->show("im/articulos/ArticuloListaForm",$vars);
    }
    
    public function shw(){
    	$this->shwForm("view");
    }
    
    public function shwForm($view=""){
		$ArticuloFrm            = new ArticuloFrm();
        $vars                   = array();
		$o_XxMaestroListaFN		= new XxMaestroListaFN(); 
        $o_XxMaestroListaFN->setListaId("tipoprod");   
		$a_tipo_articulo		= $o_XxMaestroListaFN->select();
		$o_XxMaestroListaFN->setListaId("undmedArt");   
		$a_undmedArt			= $o_XxMaestroListaFN->select();
		$o_XxMaestroListaFN->setListaId("undmedVent");   
		$a_undmedVta			= $o_XxMaestroListaFN->select();		
		$o_XxMaestroListaFN->setListaId("undmedComp");   
		$a_undmedCompra			= $o_XxMaestroListaFN->select();		
		$o_ImClasificacionFN	= new ImClasificacionFN();  
		$a_artClasificacion		= $o_ImClasificacionFN->select();
		$o_ImFabricanteFN		= new ImFabricanteFN();  
		$a_marca				= $o_ImFabricanteFN->select();		
        $ArticuloFrm->listaTipoArticulo   	= $a_tipo_articulo;       
        $ArticuloFrm->listaUndMedArticulo 	= $a_undmedArt;		
		$ArticuloFrm->listaUndMedVenta		= $a_undmedVta;		        
		$ArticuloFrm->listaUndMedCompra  	= $a_undmedCompra;
        $ArticuloFrm->listaClasificacion 	= $a_artClasificacion;        
        $ArticuloFrm->listaMarca          	= $a_marca;         
        if ($this->requestVars){
            if (array_key_exists('articuloId', $this->requestVars)) {
                if ($this->requestVars["articuloId"]){
                    $o_ImArticuloFN= new ImArticuloFN();       
                    $o_ImArticuloFN->setArticuloId($this->requestVars["articuloId"]);
                    $o_ArticuloBN = $o_ImArticuloFN->selectByPK();
                    if ($o_ArticuloBN){
                        $ArticuloFrm->articuloId        = $o_ArticuloBN->getArticuloId();
                        $ArticuloFrm->tipo              = $o_ArticuloBN->getTipo(); 
                        $ArticuloFrm->descripcion       = $o_ArticuloBN->getDescripcion();  
                        $ArticuloFrm->memo              = $o_ArticuloBN->getMemo();  
                        $ArticuloFrm->alias             = $o_ArticuloBN->getAlias();   
                        $ArticuloFrm->codFabricante     = $o_ArticuloBN->getCodFabricante();   
                        $ArticuloFrm->clasificId        = $o_ArticuloBN->getClasificId();   
                        $ArticuloFrm->subClasificId     = $o_ArticuloBN->getSubClasificId();   
                        $ArticuloFrm->marcaId           = $o_ArticuloBN->getMarcaId();   
			$ArticuloFrm->undMedida		= $o_ArticuloBN->getUoM();                           
                        $ArticuloFrm->undMedidaCompra   = $o_ArticuloBN->getUoMCompra();   
                        $ArticuloFrm->undMedidaVenta    = $o_ArticuloBN->getUoMVenta();   
                        $ArticuloFrm->undMedidaCompraFactor   = $o_ArticuloBN->getUoMCompraFactor();   
                        $ArticuloFrm->undMedidaVentaFactor    = $o_ArticuloBN->getUoMVentaFactor();                           
                        $ArticuloFrm->garantia          = $o_ArticuloBN->getGarantia();  
                        $ArticuloFrm->inventariable     = $o_ArticuloBN->getInventariable();   
                        $ArticuloFrm->ctrlSeries        = $o_ArticuloBN->getSerializable();   
                        $ArticuloFrm->paraCompra        = $o_ArticuloBN->getParaCompra();   
                        $ArticuloFrm->paraVenta         = $o_ArticuloBN->getParaVenta();   
                        $ArticuloFrm->afectoImp1        = $o_ArticuloBN->getImpuesto1Sw();   
                        $ArticuloFrm->afectoImp2        = $o_ArticuloBN->getImpuesto2Sw();   
                        $ArticuloFrm->afectoImp3        = $o_ArticuloBN->getImpuesto3Sw();   
                        $ArticuloFrm->advporc           = $o_ArticuloBN->getAdvalorem();   
                        $ArticuloFrm->peso              = $o_ArticuloBN->getPeso();   
                        $ArticuloFrm->volumen           = $o_ArticuloBN->getVolumen();
                        $ArticuloFrm->precioLista       = $o_ArticuloBN->getPrecioDefault();   
                        $ArticuloFrm->costoProm         = $o_ArticuloBN->getCostoProm();   
                        $ArticuloFrm->costoUlt          = $o_ArticuloBN->getCostoUlt();   
                        $ArticuloFrm->stockMin          = $o_ArticuloBN->getStockMin();   
                        $ArticuloFrm->stockMax          = $o_ArticuloBN->getStockMax();   
                        $ArticuloFrm->tiempoReposic     = $o_ArticuloBN->getTiempoReposic();   
                        $ArticuloFrm->activo            = $o_ArticuloBN->getActivo();   
                        $ArticuloFrm->creaUsr           = $o_ArticuloBN->getCreaUsr();   
                        $ArticuloFrm->creaFecha         = $o_ArticuloBN->getCreaFecha();   
                        $ArticuloFrm->modifUsr          = $o_ArticuloBN->getModifUsr();   
                        $ArticuloFrm->modifFecha        = $o_ArticuloBN->getModifFecha();                                                                                                
                    }
                }
            }
        }   
      
        $vars['ArticuloFrm'] = $ArticuloFrm;
        if (!isset($view) || $view==""){
        	$this->view->show("im/articulos/Articulo",$vars);
        }else{
        	$this->view->show("im/articulos/ArticuloView",$vars);
        }
    }

     public function registrarArticulo(){
         if ($this->requestVars){
                $o_ImArticuloDP  = new ImArticuloDP();                     
                $o_ImArticuloDP->setArticuloId($this->requestVars["articuloId"])  ;     
                $o_ImArticuloDP->setTipo($this->requestVars["tipoArticulo"]); 
                $o_ImArticuloDP->setDescripcion($this->requestVars["descripcion"]);  
                $o_ImArticuloDP->setMemo($this->requestVars["memo"]);  
                $o_ImArticuloDP->setAlias($this->requestVars["alias"]);   
                $o_ImArticuloDP->setCodFabricante($this->requestVars["codFabricante"]);   
                $o_ImArticuloDP->setClasificId($this->requestVars["clasificId"]);   
                $o_ImArticuloDP->setSubClasificId($this->requestVars["subClasificId"]);   
                $o_ImArticuloDP->setMarcaId($this->requestVars["marcaId"]);   
                $o_ImArticuloDP->setUoM($this->requestVars["undMedida"]);                   
                $o_ImArticuloDP->setUoMCompra($this->requestVars["undMedidaCompra"]);   
                $o_ImArticuloDP->setUoMVenta($this->requestVars["undMedidaVenta"]);   
                $o_ImArticuloDP->setUoMCompraFactor($this->requestVars["undMedidaCompraFactor"]);   
                $o_ImArticuloDP->setUoMVentaFactor($this->requestVars["undMedidaVentaFactor"]);                   
                $o_ImArticuloDP->setGarantia($this->requestVars["garantia"]);  
                if (!$this->requestVars["inventariable"]){
                    $this->requestVars["inventariable"] = 'N';
                }
                if (!$this->requestVars["ctrlSeries"]){
                    $this->requestVars["ctrlSeries"] = 'N';
                }
                if (!$this->requestVars["paraCompra"]){
                    $this->requestVars["paraCompra"] = 'N';
                }
                if (!$this->requestVars["paraVenta"]){
                    $this->requestVars["paraVenta"] = 'N';
                }            
                if (!$this->requestVars["afectoImp1"]){
                    $this->requestVars["afectoImp1"] = 'N';
                }                  
                $o_ImArticuloDP->setInventariable($this->requestVars["inventariable"]);   
                $o_ImArticuloDP->setSerializable($this->requestVars["ctrlSeries"]);   
                $o_ImArticuloDP->setParaCompra($this->requestVars["paraCompra"]);   
                $o_ImArticuloDP->setParaVenta($this->requestVars["paraVenta"]);   
                $o_ImArticuloDP->setImpuesto1Sw($this->requestVars["afectoImp1"]);   
                $o_ImArticuloDP->setImpuesto2Sw($this->requestVars["afectoImp2"]);   
                $o_ImArticuloDP->setImpuesto3Sw($this->requestVars["afectoImp3"]);   
                $o_ImArticuloDP->setAdvalorem($this->requestVars["advporc"]);   
                $o_ImArticuloDP->setPeso($this->requestVars["peso"]);   
                $o_ImArticuloDP->setVolumen($this->requestVars["volumen"]);                   
                $o_ImArticuloDP->setPrecioDefault($this->requestVars["precioLista"]);   
                $o_ImArticuloDP->setCostoProm($this->requestVars["costoProm"]);   
                $o_ImArticuloDP->setCostoUlt($this->requestVars["costoUlt"]);   
                $o_ImArticuloDP->setStockMin($this->requestVars["stockMin"]);   
                $o_ImArticuloDP->setStockMax($this->requestVars["stockMax"]);   
                $o_ImArticuloDP->setTiempoReposic($this->requestVars["tiempoReposic"]);   
                $o_ImArticuloDP->setActivo($this->requestVars["activo"]);   
                $o_ImArticuloDP->setCreaUsr($this->requestVars["creaUsr"]);   
                $o_ImArticuloDP->setCreaFecha($this->requestVars["creaFecha"]);   
                $o_ImArticuloDP->setModifUsr($this->requestVars["modifUsr"]);   
                $o_ImArticuloDP->setModifFecha($this->requestVars["modifFecha"]);                 
                if ($this->requestVars["articuloId"]){
                    $o_ImArticuloFN= new ImArticuloFN();       
                    $o_ImArticuloFN->setArticuloId($this->requestVars["articuloId"]);
                    $o_ArticuloBN = $o_ImArticuloFN->selectByPK();
                    if ($o_ArticuloBN){                	                	
                        $articuloId = $this->requestVars["articuloId"];  
                        if ($o_ImArticuloDP->update()){
                           $a_result['articuloId'] = $articuloId;
                           $a_result['rpta'] = 'true';
                        }else{
                           $a_result['msgError'] = "Error al registrar";
                        }				
                    }else{
                        $articuloId = $o_ImArticuloDP->save();
                        if ($articuloId){
                            $a_result['articuloId'] = $this->requestVars["articuloId"];
                            $a_result['rpta'] = 'true';
                           
                        }else{
                            $a_result['msgError'] = "Error al registrar";
                        }
                    }			
                }                    
         }
         echo json_encode($a_result);
     } 
     public function eliminarArticulo(){
        if ($this->requestVars["articuloId"]){
            $o_ImArticuloFN= new ImArticuloFN();       
            $o_ImArticuloFN->setArticuloId($this->requestVars["articuloId"]);
            $o_ArticuloBN = $o_ImArticuloFN->selectByPK();
            if ($o_ArticuloBN){                	                	
                $articuloId = $this->requestVars["articuloId"];  
                if ($o_ImArticuloDP->update()){
                   $a_result['articuloId'] = $articuloId;
                   $a_result['rpta'] = 'true';
                }else{
                   $a_result['msgError'] = "Error al registrar";
                }				
            }else{
                $a_result['msgError'] = "Error item no encontrado";
                
            }			
        }else{
            $a_result['msgError'] = "Error item no indicado";
        }
        echo json_encode($a_result);
     }
     
}
?>